import { hotSaleActivity, discountActivity, brandSaleActivity, activityRank, activityFull } from '@/service/get-data.js'
import { OPENEDACTIVITY, DISCOUNTACTIVITY, BRANDACTIVITY, ACTRANK } from './mutations-types'
import store from '../../'
import { getCookie } from '@/config/mUntils'
// import { resolve } from 'dns';

export default {
  // 爆品
  async fetchOpenActivity ({ commit }) {
    let res
    if (store.state.user.username !== '') {
      res = await hotSaleActivity(store.state.user.username)
    } else {
      res = await hotSaleActivity()
    }
    return new Promise((resolve, reject) => {
      if (res.data.resultCode && res.data.resultCode === 2000) {
        commit(OPENEDACTIVITY, res.data.data)
        resolve(res.data.data)
      } else {
        reject(res.data.data)
      }
    })
  },
  // 品牌折扣
  async fetchBrandActivity ({ commit }) {
    let res = await brandSaleActivity()
    return new Promise((resolve, reject) => {
      if (res.data.resultCode && res.data.resultCode === 2000) {
        commit(BRANDACTIVITY, res.data.data)
        resolve(res.data.data)
      } else {
        reject(res.data.data)
      }
    })
  },
  async fetchDiscountActivity ({ commit }, amount = 0) {
    let res = await discountActivity(amount)
    return new Promise((resolve, reject) => {
      if (res.data.resultCode && res.data.resultCode === 2000) {
        commit(DISCOUNTACTIVITY, res.data.data)
        resolve(res.data.data)
      } else {
        reject(res.data.data)
      }
    })
  },
  // 获取排名
  async getActivityRank ({ commit }, rangSize = 30) {
    let res = await activityRank(store.state.user.id || getCookie('USERID'), rangSize)
    return new Promise((resolve, reject) => {
      if (res.data.resultCode && res.data.resultCode === 2000) {
        commit(ACTRANK, res.data.data)
        resolve(res.data.data)
      } else {
        reject(res.data.data)
      }
    })
  },
  // 获取满享活动
  async getActivityFull ({ commit }) {
    let res = await activityFull(1, 10, (store.state.user.id || getCookie('USERID')))
    return new Promise((resolve, reject) => {
      if (res.data.resultCode && res.data.resultCode === 2000) {
        resolve(res.data.data)
      } else {
        reject(res.data.data)
      }
    })
  }
}
